#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>

#define CLOSE_CMD (_IO(0XEF, 0x1))     /* 关闭定时器 */
#define OPEN_CMD (_IO(0XEF, 0x2))      /* 打开定时器 */
#define SETPERIOD_CMD (_IO(0XEF, 0x3)) /* 设置定时器周期命令 */


int main(int argc, char *argv[])
{
    int fd, retvalue;
    char *filename;
    unsigned int cmd = 0;
    unsigned long arg = 0;  //定时器周期


    if((argc != 3) && (argc != 4)) {
        printf("Error Usage!\r\n");
        return -1;
    }

    filename = argv[1];

    fd = open(filename, O_RDWR);
    if(fd < 0) {
        printf("file %s open failed!\r\n", filename);
        return -1;
    }

    cmd = atoi(argv[2]); /* 将字符转换为数字 */
    switch (cmd)
    {
        case 1:cmd = OPEN_CMD; break;
        case 2:cmd = CLOSE_CMD; break;
        case 3:cmd = SETPERIOD_CMD; break;
        default:
            break;
    }
    if (cmd == SETPERIOD_CMD)
    {
        arg = atoi(argv[3]);
    }

    retvalue = ioctl(fd,cmd,arg);
    if(retvalue < 0) {
        printf("LED Control Failed!\r\n");
        close(fd);
        return -1;
    }

    close(fd);

    return 0;
}
